﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using SuperFox.SuperServiceReference;

namespace SuperFox
{

    public partial class MainPage : UserControl
    {

        private DbUsers currentUser;

        private SuperServiceClient client;

        public DbUsers CurrentUser
        {
            get
            {
                return currentUser;
            }

            set
            {
                currentUser = value;
            }
        }
        private ObservableCollection<HyperlinkButton> MainMenuLinks;

        public MainPage()
        {
            InitializeComponent();
        }

        public void LoadSidbar(UserControl bar)
        {
            SidbarHolder.Children.Clear();
            SidbarHolder.Children.Add(bar);
        }

        public void LoadContent(UserControl content)
        {
            ContentHolder.Children.Clear();
            ContentHolder.Children.Add(content);
        }

        public void LoadMenu(UserControl menu)
        {
            // TBD
        }

        public void LoadHeader(UserControl header)
        {
            HeaderControl.Children.Clear();
            HeaderControl.Children.Add(header);
        }


        private void ResetUIControls()
        {
            SidbarHolder.Children.Clear();
            ContentHolder.Children.Clear();
            HeaderControl.Children.Clear();
            //MainBackground.Children.Clear();
            //FooterRepeater.Children.Clear();
        }

        public void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {

            switch((sender as HyperlinkButton).Content as String)
            {
                case "HOME":

                    ResetUIControls();

                    SidbarHolder.Children.Add(new MainSideBarProducts());
                    ContentHolder.Children.Add(new ContentControl());
                    HeaderControl.Children.Add(new MainHeader());
                    //MainBackground.Children.Add(new BackgroundControl());
                    MainContent.Background = App.Current.Resources["BACKGROUNDCOLOR"] as SolidColorBrush;
                    LowerBackground.Background = App.Current.Resources["BACKGROUNDCOLOR"] as SolidColorBrush;
                    //FooterRepeater.Children.Add(new FooterControl());
                    FooterControlGrid.Background = App.Current.Resources["FOOTERBACKGROUND"] as SolidColorBrush;
                    //(App.Current.RootVisual as MainPage).MainTilePanel.Visibility = System.Windows.Visibility.Visible;
                    //(App.Current.RootVisual as MainPage).FooterTilePanel.Visibility = System.Windows.Visibility.Visible;

                    StyleLinks(sender as HyperlinkButton);


                break;

                case "OUR APPROACH":

                    ResetUIControls();

                    SubSideBarProducts ssbp = new SubSideBarProducts();

                    SidbarHolder.Children.Add(ssbp);

                    // decide which sub item to display

                    switch (Int32.Parse((string)(sender as HyperlinkButton).Tag))
                    {
                        case 0:
                            ContentHolder.Children.Add(new Philosophy());
                            ssbp.StyleLinks("Our Philosophy");
                            break;

                        case 1:
                            ContentHolder.Children.Add(new Makeover());
                            ssbp.StyleLinks("Merchandise Makeover");
                            break;

                        case 2:
                            ContentHolder.Children.Add(new Green());
                            ssbp.StyleLinks("Truly Green Promotional Products");
                            break;

                        default:
                            ContentHolder.Children.Add(new Philosophy());
                            ssbp.StyleLinks("Our Philosophy");
                            break;
                    }
                    
                    
                    
                    HeaderControl.Children.Add(new MainHeader());
                    //MainBackground.Children.Add(new BackgroundControl());
                    MainContent.Background = App.Current.Resources["BACKGROUNDCOLOR"] as SolidColorBrush;
                    LowerBackground.Background = App.Current.Resources["BACKGROUNDCOLOR"] as SolidColorBrush;
                    FooterControlGrid.Background = App.Current.Resources["FOOTERBACKGROUND"] as SolidColorBrush;
                    //(App.Current.RootVisual as MainPage).MainTilePanel.Visibility = System.Windows.Visibility.Visible;
                    //(App.Current.RootVisual as MainPage).FooterTilePanel.Visibility = System.Windows.Visibility.Visible;
                    //FooterRepeater.Children.Add(new FooterControl());

                    StyleLinks(sender as HyperlinkButton);
                break;

                case "PRODUCT RANGE":

                    ResetUIControls();

                    SidbarHolder.Children.Add(new SubSideBarProducts());
                    ContentHolder.Children.Add(new Products());
                    HeaderControl.Children.Add(new MainHeader());
                    //MainBackground.Children.Add(new BackgroundControl());
                    MainContent.Background = App.Current.Resources["BACKGROUNDCOLOR"] as SolidColorBrush;
                    LowerBackground.Background = App.Current.Resources["BACKGROUNDCOLOR"] as SolidColorBrush;
                    FooterControlGrid.Background = App.Current.Resources["FOOTERBACKGROUND"] as SolidColorBrush;
                    //(App.Current.RootVisual as MainPage).MainTilePanel.Visibility = System.Windows.Visibility.Visible;
                    //(App.Current.RootVisual as MainPage).FooterTilePanel.Visibility = System.Windows.Visibility.Visible;
                    //FooterRepeater.Children.Add(new FooterControl());

                    StyleLinks(sender as HyperlinkButton);
                break;

                case "MERCHANDISE PROGRAMS":
                    ResetUIControls();

                    SidbarHolder.Children.Add(new LoginSideBar());
                    ContentHolder.Children.Add(new Login());
                    HeaderControl.Children.Add(new MainHeader());
                    //MainBackground.Children.Add(new BackgroundControl());
                    MainContent.Background = App.Current.Resources["BACKGROUNDCOLOR"] as SolidColorBrush;
                    LowerBackground.Background = App.Current.Resources["BACKGROUNDCOLOR"] as SolidColorBrush;
                    FooterControlGrid.Background = App.Current.Resources["FOOTERBACKGROUND"] as SolidColorBrush;
                    //(App.Current.RootVisual as MainPage).MainTilePanel.Visibility = System.Windows.Visibility.Visible;
                    //(App.Current.RootVisual as MainPage).FooterTilePanel.Visibility = System.Windows.Visibility.Visible;
                    //.Children.Add(new FooterControl());

                    StyleLinks(sender as HyperlinkButton);
                break;

                case "TESTIMONIALS":
                    ResetUIControls();

                    SidbarHolder.Children.Add(new CAmpersSideBar());
                    ContentHolder.Children.Add(new HappyCampers());
                    HeaderControl.Children.Add(new MainHeader());

                    MainContent.Background = App.Current.Resources["BACKGROUNDCOLOR"] as SolidColorBrush;
                    LowerBackground.Background = App.Current.Resources["BACKGROUNDCOLOR"] as SolidColorBrush;
                    FooterControlGrid.Background = App.Current.Resources["FOOTERBACKGROUND"] as SolidColorBrush;
                    //(App.Current.RootVisual as MainPage).MainTilePanel.Visibility = System.Windows.Visibility.Visible;
                    //(App.Current.RootVisual as MainPage).FooterTilePanel.Visibility = System.Windows.Visibility.Visible;

                    StyleLinks(sender as HyperlinkButton);
                break;

                case "CONTACT US":
                    ResetUIControls();

                    SidbarHolder.Children.Add(new GoodCallSideBar());
                    ContentHolder.Children.Add(new GoodCall());
                    HeaderControl.Children.Add(new MainHeader());

                    MainContent.Background = App.Current.Resources["BACKGROUNDCOLOR"] as SolidColorBrush;
                    LowerBackground.Background = App.Current.Resources["BACKGROUNDCOLOR"] as SolidColorBrush;
                    FooterControlGrid.Background = App.Current.Resources["FOOTERBACKGROUND"] as SolidColorBrush;
                    //(App.Current.RootVisual as MainPage).MainTilePanel.Visibility = System.Windows.Visibility.Visible;
                    //(App.Current.RootVisual as MainPage).FooterTilePanel.Visibility = System.Windows.Visibility.Visible;

                    StyleLinks(sender as HyperlinkButton);
                break;

            }

        }

        private void StyleLinks(HyperlinkButton activeLink)
        {
            if (MainMenuLinks == null)
            {
                return;
            }

            foreach (HyperlinkButton hl in MainMenuLinks)
            {
                if (hl == activeLink)
                {
                    hl.Style = App.Current.Resources["MainMenuItemSelected"] as Style;
                }
                else
                {
                    hl.Style = App.Current.Resources["MainMenuItem"] as Style;
                }
            }

        }


        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            MainMenuLinks = new ObservableCollection<HyperlinkButton>();
            MainMenuLinks.Add(HomeLink);
            MainMenuLinks.Add(AboutLink);
            MainMenuLinks.Add(ProductLink);
            MainMenuLinks.Add(StuffLink);
            MainMenuLinks.Add(CamperLink);
            MainMenuLinks.Add(GoodCallLink);
            

            SidbarHolder.Children.Add(new MainSideBarProducts());
            ContentHolder.Children.Add(new ContentControl());

            StyleLinks(HomeLink);

            /*
            SidbarHolder.Children.Add(new LoginSideBar());
            ContentHolder.Children.Add(new Login());

            StyleLinks(StuffLink);*/



            // now we need to see if an order is being authorised

            try
            {
                int id = Int32.Parse(System.Windows.Browser.HtmlPage.Document.QueryString["orderid"]);
                string action = System.Windows.Browser.HtmlPage.Document.QueryString["action"];

                if (id == 0)
                    return;

                client = ServiceConfig.Connect();

                if (action == "au")
                {

                    client.AuthoriseOrderCompleted += new EventHandler<AuthoriseOrderCompletedEventArgs>(client_AuthoriseOrderCompleted);
                    client.AuthoriseOrderAsync(id);

                }
                else if (action == "de")
                {
                    client.DeclineOrderCompleted += new EventHandler<DeclineOrderCompletedEventArgs>(client_DeclineOrderCompleted);
                    client.DeclineOrderAsync(id);
                }
                else
                {
                    AuthorizedControl ac = new AuthorizedControl();

                    ac.StatusText.Text = "No action found";

                    this.LoadContent(ac);

                    System.Windows.Browser.HtmlPage.Document.QueryString.Clear();

                }
            }catch(Exception)
            {}
        }

        void client_DeclineOrderCompleted(object sender, DeclineOrderCompletedEventArgs e)
        {
            AuthorizedControl ac = new AuthorizedControl();

            if (e.Error != null)
            {
                ac.StatusText.Text = "There was an error updateing this order status. Please contact superfox support";
            }
            else
            {
                ac.StatusText.Text = e.Result;
            }

            this.LoadContent(ac);

            System.Windows.Browser.HtmlPage.Document.QueryString.Clear();
        }

        void client_AuthoriseOrderCompleted(object sender, AuthoriseOrderCompletedEventArgs e)
        {
            AuthorizedControl ac = new AuthorizedControl();

            if (e.Error != null)
            {
                ac.StatusText.Text = "There was an error updateing this order status. Please contact superfox support";
            }
            else
            {
                ac.StatusText.Text = e.Result;
            }

            this.LoadContent(ac);

            System.Windows.Browser.HtmlPage.Document.QueryString.Clear();
        }

        private void ScrollViewer_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            (sender as ScrollViewer).ScrollToVerticalOffset((-1 * e.Delta) + (sender as ScrollViewer).VerticalOffset);
        }


        private void PrivaceLink_Click(object sender, RoutedEventArgs e)
        {
            ModalPopup.Show(new Terms(), "Privacy Policy");
        }

        private void TermsLink_Click(object sender, RoutedEventArgs e)
        {
            ModalPopup.Show(new PrivacyPolicy(), "Terms & Conditions");
        }

    }
}
